//
//  LocalNotification.m
//  MISCloud
//
//  Created by goldeney on 13-8-15.
//  Copyright (c) 2013年 goldeneye. All rights reserved.
//

#import "LocalNotification.h"

@implementation LocalNotification

static NSMutableDictionary *notificationDic;

/*******只发送消息*********/
+ (void) messageLocalNotification:(NSString*) message alertAction:(NSString*) alertAction
{
    Class cls = NSClassFromString(@"UILocalNotification");
    if (cls != nil) {
        UILocalNotification *notification = [[UILocalNotification alloc] init];
        //触发通知的时间
        //NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        //[formatter setDateFormat:@"HH:mm:ss"];
        //NSDate *now = [formatter dateFromString:@"14:25:00"];
        NSDate *now = [NSDate date];
        notification.fireDate = now;
        //时区
        notification.timeZone = [NSTimeZone defaultTimeZone];
        //通知重复提示的单位，可以是天、周、月
        notification.repeatInterval = NSDayCalendarUnit;
        //通知内容
        if (message) notification.alertBody = message;
        if (alertAction) notification.alertAction = alertAction;
        //通知被触发时播放的声音
        notification.soundName = UILocalNotificationDefaultSoundName;
        //执行通知注册
        [[UIApplication sharedApplication] scheduleLocalNotification:notification];
    }
}

/*******发送消息并传递数据*********/
+ (void) messageLocalNotification:(NSString*) message objectValue:(id) value objectForKey:(NSString*) key
{
    if (!notificationDic)
        notificationDic = [[NSMutableDictionary alloc] init];
    
    Class cls = NSClassFromString(@"UILocalNotification");
    if (cls != nil) {
        UILocalNotification *notification = [[UILocalNotification alloc] init];
        //触发通知的时间
        //NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        //[formatter setDateFormat:@"HH:mm:ss"];
        //NSDate *now = [formatter dateFromString:@"14:25:00"];
        //NSDate *now = [NSDate date];
        //notification.fireDate = now;
        //时区
        //notification.timeZone = [NSTimeZone defaultTimeZone];
        //通知重复提示的单位，可以是天、周、月
        //notification.repeatInterval = NSDayCalendarUnit;
        //通知内容
        if (message) notification.alertBody = message;
        //通知被触发时播放的声音
        notification.soundName = UILocalNotificationDefaultSoundName;
        //添加数据
        if (value && key && [value isKindOfClass:[NSDictionary class]]) {
            NSDictionary *userDict = [NSDictionary dictionaryWithObject:value forKey:key];
            notification.userInfo = userDict;
        }
        
        //清理之前显示的通知
        UILocalNotification *oldNotification = [notificationDic objectForKey:key];
        [self cancelLocalNotification:oldNotification];
        if (key && notification) {
            [notificationDic setObject:notification forKey:key];
        }
        
        //执行通知注册
        [[UIApplication sharedApplication] presentLocalNotificationNow:notification];
    }
}

/*******应用未运行的情况，点击获取通知数据*******/
+ (id) appNotRunLocalNotification:(NSDictionary *)launchOptions objectForKey:(NSString*) key
{
    //取消APP标记
    UIApplication *application = [UIApplication sharedApplication];
    application.applicationIconBadgeNumber = 0;
    
    Class cls = NSClassFromString(@"UILocalNotification");
    if (cls) {
        UILocalNotification *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
        if (notification) {
            id reminder = [notification.userInfo objectForKey:key];
            return reminder;
        }
    }
    
    return nil;
}

/*******APP图标标记*******/
+ (void) setAPPIconBadgeNumber:(NSInteger) number
{
    UIApplication *application = [UIApplication sharedApplication];
    application.applicationIconBadgeNumber = number;
}

/*******取消指定通知*******/
+ (void) cancelLocalNotification:(UILocalNotification *) notification
{
    if (notification){
        [[UIApplication sharedApplication] cancelLocalNotification:notification];
    }
}

/*******取消所有通知消息*******/
+ (void) cancelAllLocalNotifications
{
    [notificationDic removeAllObjects];
    [[UIApplication sharedApplication] cancelAllLocalNotifications];
}

@end
